package cn.com.generaldata.os.service.indicator_evaluation.monitoring;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.random.R;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.pojo.IndicatorEvaluationResult;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 二期预警指数评估指标计算类-针对信号灯图-海洋生产总值增加值计算类
 * 2017-6-13
 */
public class HysczzCalculateService extends IndicatorEvaluationCalculateService {


	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double sum = 0d;
		
			//获取基础指标值-从录入指标或核算指标取
			Double warningIndicatorVal = getInputOrAccountingIndicatorValue(indicatorAccountVO,
					indicatorAccountVO.getYear(), indicator.getReference());//选择年海洋生产总值
			
			//计算生产总值增加值
			Double oldVal = getInputOrAccountingIndicatorValue(indicatorAccountVO,
					indicatorAccountVO.getYear()-1, indicator.getReference());//去年海洋生产总值
			if (oldVal==null || oldVal.equals("")) {
				oldVal=0d;
			}
			Double newVal = warningIndicatorVal-oldVal;
			//从tbl_indicator_evaluation表指标id
			Integer warningIndicatorId = getIndicatorEvaluationId(indicator.getName(), indicatorAccountVO.getAreaId());
		//	Double warningIndicatorVal = getWarningIndicatorVal(indicatorAccountVO, warningIndicatorId);//tbl_indicator_evaluation_res
			String result = getResultValue(warningIndicatorId, newVal);//最大最小值:tbl_indicator_conclusion
			if (result == null) {
				Double maxValue = getEvaluationMaxValue(warningIndicatorId);
				Double minValue = getEvaluationMinValue(warningIndicatorId);
				if (maxValue < newVal) {
					result = "5";
				} else if (minValue > newVal) {
					result = "1";
				} else {
					String resultMaxValue = getResultMaxValue(warningIndicatorId, newVal);
					String resultMinValue = getResultMinValue(warningIndicatorId, newVal);
					if (resultMaxValue != null) {
						if ("4".equals(resultMaxValue)) {
							result = "5";
						}
						if ("2".equals(resultMaxValue)) {
							result = "2";
						}
					}
					if (resultMinValue != null) {
						if ("2".equals(resultMinValue)) {
							result = "1";
						}
						if ("4".equals(resultMinValue)) {
							result = "4";
						}
					}
				}
			}
	
		return Double.valueOf(result);
	}

	protected String getResultMaxValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_max=" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected String getResultMinValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_min=" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected String getResultValue(Integer warningIndicatorId, Double value) {
		String result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select result from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId
					+ " and value_min<" + value + " and value_max>" + value;
			result = sqlTemplate.queryForObject(sql, null, String.class);
		}
		return result;
	}

	protected Double getEvaluationMaxValue(Integer warningIndicatorId) {
		Double result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			String sql = "select max(value_max) from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId;
			result = sqlTemplate.queryForObject(sql, null, Double.class);
		}
		return result;
	}

	protected Double getEvaluationMinValue(Integer warningIndicatorId) {
		Double result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if (warningIndicatorId != null) {
			// "select result from tbl_indicator_conclusion where indicator_id='306' and value_min<=0.17 and value_max>0.17";
			String sql = "select min(value_min) from tbl_indicator_conclusion where indicator_id=" + warningIndicatorId;
			result = sqlTemplate.queryForObject(sql, null, Double.class);
		}
		return result;
	}

 

}
